package com.yalemang.photovalut.base.helper

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import android.graphics.drawable.shapes.RoundRectShape
import android.graphics.drawable.shapes.Shape
import com.blankj.utilcode.util.ConvertUtils

class CommonDrawableHelper {

    //Drawable形状
    private var shapeType: ShapeType? = null

    //Drawable左上圆矩
    private var leftTopRadius = 0f

    //Drawable左下圆矩
    private var leftBottomRadius = 0f

    //Drawable右上圆矩
    private var rightTopRadius = 0f

    //Drawable右下圆矩
    private var rightBottomRadius = 0f

    //颜色
    private var color = 0

    //边框颜色
    private var strokeColor = -1

    //边框宽度
    private var strokeWidth = -1

    enum class ShapeType {
        Oval,
        RoundRectangle
    }

    private fun buildDrawable(): Drawable {
        return if (shapeType == ShapeType.Oval) {
            val shapeDrawable = ShapeDrawable()
            shapeDrawable.setShape(OvalShape())
            shapeDrawable.paint.setColor(color)
            shapeDrawable
        } else {
            leftTopRadius = dpToPx(leftTopRadius)
            leftBottomRadius = dpToPx(leftBottomRadius)
            rightTopRadius = dpToPx(rightTopRadius)
            rightBottomRadius = dpToPx(rightBottomRadius)
            val externalRound = floatArrayOf(
                leftTopRadius, leftTopRadius,
                rightTopRadius, rightTopRadius,
                rightBottomRadius, rightBottomRadius,
                leftBottomRadius, leftBottomRadius
            )
            val distanceRectF = RectF(0f, 0f, 0f, 0f)
            val insideRound = floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f)
            CommonDrawable(
                externalRound,
                distanceRectF,
                insideRound,
                dpToPx(strokeWidth.toFloat()),
                color,
                strokeColor
            )
        }
    }

    private fun dpToPx(dp: Float): Float {
        return ConvertUtils.dp2px(dp).toFloat()
    }

    class Builder {
        //Drawable形状
        private var shapeType = ShapeType.RoundRectangle

        //Drawable左上圆矩
        private var leftTopRadius = 0f

        //Drawable左下圆矩
        private var leftBottomRadius = 0f

        //Drawable右上圆矩
        private var rightTopRadius = 0f

        //Drawable右下圆矩
        private var rightBottomRadius = 0f

        //画笔颜色
        private var color = 0

        //边框颜色
        private var strokeColor = 0

        //边框宽度
        private var strokeWidth = 0
        fun setShapeType(shapeType: ShapeType): Builder {
            this.shapeType = shapeType
            return this
        }

        fun setLeftTopRadius(leftTopRadius: Float): Builder {
            this.leftTopRadius = leftTopRadius
            return this
        }

        fun setLeftBottomRadius(leftBottomRadius: Float): Builder {
            this.leftBottomRadius = leftBottomRadius
            return this
        }

        fun setRightTopRadius(rightTopRadius: Float): Builder {
            this.rightTopRadius = rightTopRadius
            return this
        }

        fun setRightBottomRadius(rightBottomRadius: Float): Builder {
            this.rightBottomRadius = rightBottomRadius
            return this
        }

        fun setTop(top: Float): Builder {
            leftTopRadius = top
            rightTopRadius = top
            return this
        }

        fun setBottom(bottom: Float): Builder {
            leftBottomRadius = bottom
            rightBottomRadius = bottom
            return this
        }

        fun setAll(dp: Float): Builder {
            setTop(dp)
            setBottom(dp)
            return this
        }

        fun setLeftDiagonal(dp: Float): Builder {
            leftTopRadius = dp
            rightBottomRadius = dp
            return this
        }

        fun setRightDiagonal(dp: Float): Builder {
            rightTopRadius = dp
            leftBottomRadius = dp
            return this
        }

        fun setColor(color: Int): Builder {
            this.color = color
            return this
        }

        fun setStrokeColor(strokeColor: Int): Builder {
            this.strokeColor = strokeColor
            return this
        }

        fun setStrokeWidth(strokeWidth: Int): Builder {
            this.strokeWidth = strokeWidth
            return this
        }

        fun builder(): Drawable {
            val commonDrawableHelper = CommonDrawableHelper()
            commonDrawableHelper.shapeType = shapeType
            commonDrawableHelper.leftTopRadius = leftTopRadius
            commonDrawableHelper.leftBottomRadius = leftBottomRadius
            commonDrawableHelper.rightTopRadius = rightTopRadius
            commonDrawableHelper.rightBottomRadius = rightBottomRadius
            commonDrawableHelper.color = color
            commonDrawableHelper.strokeColor = strokeColor
            commonDrawableHelper.strokeWidth = strokeWidth
            return commonDrawableHelper.buildDrawable()
        }
    }


    private class CommonDrawable(
        outerRadii: FloatArray?,
        inset: RectF?,
        innerRadii: FloatArray?,
        strokeWidth: Float,
        color: Int,
        strokeColor: Int
    ) :
        ShapeDrawable(RoundRectShape(outerRadii, inset, innerRadii)) {
        private var strokePaint: Paint? = null
        private val fillPaint: Paint

        init {
            fillPaint = Paint(this.paint)
            fillPaint.setColor(color)
            if (strokeWidth > 0) {
                strokePaint = Paint(fillPaint)
                strokePaint!!.style = Paint.Style.STROKE
                strokePaint!!.strokeWidth = strokeWidth
                strokePaint!!.setColor(strokeColor)
            }
        }

        override fun onDraw(shape: Shape, canvas: Canvas, paint: Paint) {
            shape.draw(canvas, fillPaint)
            if (strokePaint != null) {
                shape.draw(canvas, strokePaint)
            }
        }
    }

}